/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newPostPanel.java
 *
 * Created on 22/Out/2012, 9:58:34
 */
package chatclient;

import com.sd.chat.ImageFileView;
import com.sd.chat.ImageFilter;
import com.sd.chat.ImagePreview;
import com.sd.chat.Logger;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFileChooser;
import org.jdesktop.application.Action;

/**
 *
 * @author Old
 */
public class newPostPanel extends javax.swing.JPanel {

    public ChatSDClientView pai;
    private JFileChooser jFileChooser1;
    private Logger log = new Logger(true);
    private File selectedPic;

    /** Creates new form newPostPanel */
    public newPostPanel() {
        initComponents();
        this.jFileChooser1 = new JFileChooser();
    }

    public newPostPanel(ChatSDClientView pai) {
        this.pai = pai;
        initComponents();
        this.jFileChooser1 = new JFileChooser();

        dateField.setValue(new Date());
        timeField.setValue(new Date());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        postIsNow = new javax.swing.JCheckBox();
        dateField = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        postHasPic = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        postText = new javax.swing.JTextArea();
        picUrl = new javax.swing.JTextField();
        choosePic = new javax.swing.JButton();
        butGuardar = new javax.swing.JButton();
        butCancelar = new javax.swing.JButton();
        timeField = new javax.swing.JFormattedTextField();

        setMaximumSize(new java.awt.Dimension(480, 100));
        setMinimumSize(new java.awt.Dimension(480, 100));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(480, 100));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(chatclient.ChatSDClientApp.class).getContext().getResourceMap(newPostPanel.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        postIsNow.setSelected(true);
        postIsNow.setText(resourceMap.getString("postIsNow.text")); // NOI18N
        postIsNow.setName("postIsNow"); // NOI18N

        dateField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT))));
        dateField.setName("dateField"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        postHasPic.setText(resourceMap.getString("postHasPic.text")); // NOI18N
        postHasPic.setName("postHasPic"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        postText.setColumns(20);
        postText.setRows(5);
        postText.setName("postText"); // NOI18N
        jScrollPane1.setViewportView(postText);

        picUrl.setText(resourceMap.getString("picUrl.text")); // NOI18N
        picUrl.setName("picUrl"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(chatclient.ChatSDClientApp.class).getContext().getActionMap(newPostPanel.class, this);
        choosePic.setAction(actionMap.get("seleccionarImagemBut")); // NOI18N
        choosePic.setText(resourceMap.getString("choosePic.text")); // NOI18N
        choosePic.setName("choosePic"); // NOI18N

        butGuardar.setAction(actionMap.get("submitPostAction")); // NOI18N
        butGuardar.setText(resourceMap.getString("butGuardar.text")); // NOI18N
        butGuardar.setName("butGuardar"); // NOI18N

        butCancelar.setAction(actionMap.get("limparAction")); // NOI18N
        butCancelar.setText(resourceMap.getString("butCancelar.text")); // NOI18N
        butCancelar.setName("butCancelar"); // NOI18N

        timeField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance())));
        timeField.setName("timeField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(postIsNow)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateField))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(picUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(choosePic))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(8, 8, 8)
                                .addComponent(postHasPic)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(butCancelar)
                    .addComponent(butGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(timeField))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(postIsNow)
                            .addComponent(dateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(timeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(picUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(choosePic)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(butGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(butCancelar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(postHasPic)
                            .addComponent(jLabel5))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void seleccionarImagemBut() {
        this.jFileChooser1.setFileFilter(new ImageFilter());
        this.jFileChooser1.setFileView(new ImageFileView());

        //Add the preview pane.
        this.jFileChooser1.setAccessory(new ImagePreview(this.jFileChooser1));
        int returnVal = this.jFileChooser1.showOpenDialog(this);
        if (returnVal == 0) {

//    this.uploadFiles = new File[1];
            selectedPic = (File) this.jFileChooser1.getSelectedFile();


            /*File[] newFileArr = new File[tempFiles.length];
            String[] filenames = new String[tempFiles.length];
            for(int j=0; j<tempFiles.length; j++){
            filenames[j] = tempFiles[j].getAbsolutePath();
            System.out.println(filenames[j]);
            newFileArr[j] = new File(filenames[j]);
            }*/


            this.jFileChooser1.setSelectedFile(null);

        }
    }

    @Action
    public void submitPostAction() {
        String texto = postText.getText();
        if (texto.length() > 0) {
            boolean isNow = postIsNow.isSelected();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy H:mm:ss");
            try {
                GregorianCalendar pDate = new GregorianCalendar();
                if(!isNow){
                    pDate.setTime(dateFormat.parse(dateField.getText()+" "+timeField.getText()));
                }
                
                boolean hasPic = postHasPic.isSelected();
                String picLoc = "";
                if(hasPic){
                    picLoc = selectedPic.getAbsolutePath();
                }
                pai.newPostSubmit(texto, hasPic, picLoc, pDate);
                pai.resetToolPanel();
            } catch (ParseException ex) {
                log.erro("newPostPanel Data inválida.");
            }
        }

    }

    @Action
    public void limparAction() {
        pai.resetToolPanel();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butCancelar;
    private javax.swing.JButton butGuardar;
    private javax.swing.JButton choosePic;
    private javax.swing.JFormattedTextField dateField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField picUrl;
    private javax.swing.JCheckBox postHasPic;
    private javax.swing.JCheckBox postIsNow;
    private javax.swing.JTextArea postText;
    private javax.swing.JFormattedTextField timeField;
    // End of variables declaration//GEN-END:variables
}
